package com.cy.myblog.service.impl;

import com.cy.myblog.dao.UserDaoI;
import com.cy.myblog.model.Tuser;
import com.cy.myblog.pageModel.DataGrid;
import com.cy.myblog.pageModel.User;
import com.cy.myblog.service.UserServiceI;
import com.cy.myblog.util.Encrypt;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

@Service("userService")
public class UserServiceImpl implements UserServiceI{

	private UserDaoI userDao;
	
	public UserDaoI getUserDao() {
		return userDao;
	}

	@Resource(name="userDynamicDao")
	public void setUserDao(UserDaoI userDao) {
		this.userDao = userDao;
	}

	public void save(User user) {
		user.setId(UUID.randomUUID().toString());
		user.setCreatime(new Date());
		user.setPassword(Encrypt.md5(user.getPassword()));
		Tuser tuser = new Tuser();
		BeanUtils.copyProperties(user, tuser);
		userDao.save(tuser);
	}

	public Tuser getById(String id){
		return userDao.getById(Tuser.class, id);
	}

	@Override
	public Tuser getByHql(User user) {
		String hql = "from Tuser where account = :account and password = :password";
		if(user != null){
			Map<String,Object> params = new HashMap<String,Object>();
			params.put("account", user.getAccount());
			params.put("password", user.getPassword());
			return userDao.getByHql(hql, params);
		}
		return null;
	}

	public Tuser getByHql(String account){
		String hql = "from Tuser where account = :account";
		if("".equals(account) || account == null){
			return null;
		}

		Map<String,Object> params = new HashMap<String,Object>();
		params.put("account", account);
		return userDao.getByHql(hql, params);
	}

	@Override
	public Tuser update(User user) {
		Tuser tu = getById(user.getId());
		BeanUtils.copyProperties(user, tu, new String[]{"id", "account", "password", "creatime", "logintime", "ipaddress", "state"});
		return tu;
	}

	@Override
	public void update(Tuser user) {
		userDao.update(user);
	}

	@Override
	public void delete(String id) {
		Tuser user = this.getById(id);
		userDao.delete(user);
	}

	@Override
	public int delete(User user) {
		String hql = "delete from Tuser where id in (";
		String[] ids = user.getIds().split(",");
		if (ids.length > 0) {
			for (int i = 0; i < ids.length; i++) {
				if (i == 0) {
					hql += "'" + ids[i] + "'";
				}
				else {
					hql += ",'" + ids[i] + "'";
				}
			}
		}
		hql += ")";
		return userDao.executeHql(hql);
	}

	@Override
	public DataGrid findByPage(User user) {
		String hql = "from Tuser t where 1=1";
		Map<String, Object> params = new HashMap<String, Object>();
		hql = addWhere(user, hql, params);              // "from Tuser t where 1=1 and t.account like %:account%"
		String hql_total = "select count(*) " + hql;    // "select count(*) from Tuser t where 1=1 and t.account like %:account%"
		hql = addOrder(user, hql);                      // "from Tuser t where 1=1 and t.account like %:account% order by account asc"

		Long count = userDao.count(hql_total,params);
		DataGrid dataGrid = new DataGrid();
		List<Tuser> l = userDao.find(hql,params,user.getPage(),user.getRows());
		List<User> nl = new ArrayList<User>();
		changModel(l, nl);
		dataGrid.setTotal(count);
		dataGrid.setRows(nl);
		return dataGrid;
	}

	private void changModel(List<Tuser> l, List<User> nl) {
		if (l != null && l.size() > 0) {
			for (Tuser o : l) {
				User u = new User();
				BeanUtils.copyProperties(o, u,new String[]{"password","ipaddress", "state"});
				nl.add(u);
			}
		}
	}

	private String addWhere(User user, String hql, Map<String, Object> params) {
		if (user.getAccount() != null && !user.getAccount().trim().equals("")) {
			hql += " and t.account like :account ";
			params.put("account", "%" + user.getAccount() + "%");
		}
		return hql;
	}

	private String addOrder(User user, String hql) {
		if (user.getSort() != null && user.getOrder() != null) {
			hql += "order by " + user.getSort() + " " + user.getOrder();
		}
		return hql;
	}
}
